package tmi.li.txwidgets.txwthr;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.util.Log;
import tmi.li.txwidgets.Widget;

public class TxWthrWidget extends Widget {
	private static final String TAG = "tmi.li.txwidgets.txwthr.TxWthrWidget";
	private Context context;
	
	public String _woeid;
	public String _current;
	public String _desc;
	public String _desc2;
	public String _lang;
	public String _format;
	private DBAdapter db;

	public TxWthrWidget(Context context) {
			this.context = context;		
	}
	
	protected void finalize() {
		try {
			db.close();
		}catch(Exception e) {
			
		}
	}
	
	
	public boolean save() {
		db = new DBAdapter(context);
		db.insertWidget(this);
		db.close();
		return true;
	}
	
	public boolean update() {
		db = new DBAdapter(context);
		boolean test = db.updateWidget(this);
		db.close();
		return test;
	}
	
	public void deleteWidget() {
		db = new DBAdapter(context);
		db.deleteWidget(this);
		db.close();
	}
	
	public boolean load(int id){
		
		try {
			db = new DBAdapter(context);
			Cursor s = db.getWidget(id);
			_id = s.getInt(0);
			_flcolor = s.getString(1);
			_ftcolor = s.getString(2);
			_fbcolor = s.getString(3);
			_bcolor = s.getString(4);
			_appname = s.getString(5);
			_woeid = s.getString(6);
			_current = s.getString(7);
			_desc = s.getString(8);
			_desc2 = s.getString(9);
			_lang = s.getString(10);
			_format = s.getString(11);
			s.close();
			db.close();
			return true;
		}catch(SQLException e) {
			//e.printStackTrace();
			return false;
		}catch(Exception e) {
			//e.printStackTrace();
			return false;
		}
		
		
	}
	
	public ContentValues getValues() {
		ContentValues values = new ContentValues();
		values.put(DBAdapter.ID, _id);
		values.put(DBAdapter.FLCOLOR, _flcolor);
		values.put(DBAdapter.FTCOLOR, _ftcolor);
		values.put(DBAdapter.FBCOLOR, _fbcolor);
		values.put(DBAdapter.BCOLOR, _bcolor);
		values.put(DBAdapter.APPNAME, _appname);
		values.put(DBAdapter.WOEID, _woeid);
		values.put(DBAdapter.CURRENT, _current);
		values.put(DBAdapter.DESC, _desc);
		values.put(DBAdapter.DESC2, _desc2);
		values.put(DBAdapter.LANG, _lang);
		values.put(DBAdapter.FORMAT, _format);
		return values;
	}
	
	public void logWidget() {
		Log.d(TAG, "id\t=>\t"+_id);
		Log.d(TAG, "FLCOLOR\t=>\t"+_flcolor);
		Log.d(TAG, "FTCOLOR\t=>\t"+_ftcolor);
		Log.d(TAG, "FBCOLOR\t=>\t"+_fbcolor);
		Log.d(TAG, "BCOLOR\t=>\t"+_bcolor);
		Log.d(TAG, "APPNAME\t=>\t"+_appname);
		Log.d(TAG, "WOEID\t=>\t"+_woeid);
		Log.d(TAG, "CURRENT\t=>\t"+_current);
		Log.d(TAG, "DESC\t=>\t"+_desc);
		Log.d(TAG, "DESC2\t=>\t"+_desc2);
		Log.d(TAG, "LANG\t=>\t"+_lang);
		Log.d(TAG, "FORMAT\t=>\t"+_format);

	}
}
